﻿using System;
using System.Threading;
using System.Collections.Generic;

using LeanEngine;

namespace LeanEngine.Data
{
    internal struct DataTransactionPool
    {
        #region Declaration Members

        private static Dictionary<int, DataTransaction> transList = new Dictionary<int, DataTransaction>(10);

        #endregion

        #region Method Members

        internal static bool HasTransaction()
        {
            return (transList == null ? false : transList.ContainsKey(Thread.CurrentThread.ManagedThreadId));
        }

        internal static DataTransaction GetTransaction()
        {
            if (transList == null || !transList.ContainsKey(Thread.CurrentThread.ManagedThreadId)) return null;

            return transList[Thread.CurrentThread.ManagedThreadId];
        }

        internal static void BeginTransaction(DataTransaction transaction)
        {
            lock (transList)
            {
                transList.Add(Thread.CurrentThread.ManagedThreadId, transaction);
            }
        }

        internal static void ReleaseTransaction()
        {
            if (transList != null)
            {
                lock (transList)
                {
                    transList.Remove(Thread.CurrentThread.ManagedThreadId);
                }
            }
        }

        #endregion
    }
}
